home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Technology Seed / ADC Seed CD - July 1999.toast / Carbon SDK 1.0d10c3 / Sample Code / BasicApp / BasicApp.c next >
Encoding:
C/C++ Source or Header  |  1999-02-17  |  4.6 KB  |  206 lines  |  [TEXT/CWIE]

  1. /* BasicApp.c */
  2.  
  3. #define TARGET_CARBON 1
  4.  
  5. #include <Events.h>                        /* SystemClick */
  6. #include <Quickdraw.h>                    /* InitGraf,InitCursor */
  7. #include <Menus.h>                        /* InitMenus */
  8. #include <Fonts.h>                        /* InitFonts */
  9. #include <MacTypes.h>                        /* Rect struct typedef */
  10. #include <Sound.h>                        /* SysBeep */
  11. #include <SegLoad.h>                    /* ExitToShell */
  12. #include <ToolUtils.h>                     /* for HiWord,LoWord */
  13. #include <MacWindows.h>
  14.  
  15. void Initialize(void);                    /* function prototypes */
  16. void EventLoop(void);
  17. void MakeWindow(void);
  18. void MakeMenu(void);
  19. void DoEvent(EventRecord *event);
  20. void DoMenuCommand(long menuResult);
  21. void DoAboutBox(void);
  22.  
  23. #define    rMenuBar                128        /* menu bar */
  24. #define    mApple                    128        /* Apple menu */
  25. #define    iAbout                    1
  26. #define    mFile                    129        /* File menu */
  27. #define    iNew                    1
  28. #define    iClose                    4
  29. #define    iQuit                    11
  30. #define    mEdit                    130        /* Edit menu */
  31. #define    mHello                    131        /* Hello menu */
  32. #define    iThis                    1
  33. #define    iThat                    2
  34. #define kAboutBox                200        /* Dialog resource for About box */
  35.  
  36. WindowPtr    myWindow;                    /* global */
  37.  
  38. void main()
  39. {
  40.     Initialize();
  41.     MakeWindow();
  42.     MakeMenu();
  43.     
  44.     EventLoop();
  45. }
  46.  
  47. void Initialize()                        /* Initialize some managers */
  48. {
  49.     InitCursor();
  50. }
  51.  
  52. void EventLoop()
  53. {
  54.     Boolean        gotEvent;
  55.     EventRecord    event;
  56.     
  57.     do
  58.     {
  59.         gotEvent = WaitNextEvent(everyEvent,&event,0,nil);
  60.         if (gotEvent)
  61.             DoEvent(&event);
  62.     } while (true);                    /* loop forever */
  63. }
  64.  
  65. void MakeWindow()                        /* Put a window */
  66. {
  67.     Rect        wRect;
  68.  
  69.     SetRect(&wRect,50,50,600,200);        /* left, top, right, bottom */
  70.     myWindow = NewWindow(nil, &wRect, "\pHello", true, zoomNoGrow, (WindowPtr) -1, false, 0);
  71.     if (myWindow == nil)
  72.     {
  73.         ExitToShell();
  74.     }
  75.     else
  76.         SetPort(GetWindowPort(myWindow));    /* set port to new window */
  77. }
  78.  
  79. void MakeMenu()                                    /* Put up a menu */
  80. {
  81.     Handle        menuBar;
  82.     
  83.     menuBar = GetNewMBar(rMenuBar);                /* read menus into menu bar */
  84.     if ( menuBar != nil )
  85.     {
  86.         SetMenuBar(menuBar);                    /* install menus */
  87. //        DisposHandle(menuBar);
  88.         AppendResMenu(GetMenuHandle(mApple), 'DRVR');    /* add DA names to Apple menu */
  89.         DrawMenuBar();
  90.     }
  91.     if ( menuBar == nil )
  92.     {
  93.         DebugStr("\pMakeMenu");
  94.         ExitToShell();
  95.     }
  96. }
  97.  
  98. void DoEvent(EventRecord *event)
  99. {
  100.     short            part;
  101.     Boolean            hit;
  102.     char            key;
  103.     Rect            tempRect;
  104.  
  105.     switch (event->what) 
  106.     {
  107.         case mouseDown:
  108.             part = FindWindow(event->where, &myWindow);
  109.             switch (part)
  110.             {
  111.                 case inMenuBar:                  /* process a moused menu command */
  112.                     DoMenuCommand(MenuSelect(event->where));
  113.                     break;
  114.                 case inSysWindow:               /* let system handle it */
  115.                     SystemClick(event, myWindow);
  116.                     break;
  117.                 case inContent:
  118.                     if (myWindow != FrontWindow()) 
  119.                         SelectWindow(myWindow);
  120.                     break;
  121.                 case inDrag:                    /* pass screenBits.bounds */
  122.                     GetRegionBounds( GetGrayRgn(), &tempRect );
  123.                     DragWindow(myWindow, event->where, &tempRect);
  124.                     break;
  125.                 case inGrow:
  126.                     break;
  127.                 case inZoomIn:
  128.                 case inZoomOut:
  129.                     hit = TrackBox(myWindow, event->where, part);
  130.                     if (hit) 
  131.                     {
  132.                         SetPort(GetWindowPort(myWindow));      /* window must be current port */
  133.                         EraseRect(GetWindowPortBounds(myWindow, &tempRect));  /* b/c ZoomWindow bug */
  134.                         ZoomWindow(myWindow, part, true); /* we inval/erase to */
  135.                         InvalWindowRect(myWindow,GetWindowPortBounds(myWindow, &tempRect));              /* look better */
  136.                     }
  137.                     break;
  138.             }
  139.             break;
  140.         case keyDown:
  141.         case autoKey:
  142.             key = event->message & charCodeMask;
  143.             if (event->modifiers & cmdKey)
  144.                 if (event->what == keyDown)
  145.                     DoMenuCommand(MenuKey(key));
  146.         case activateEvt:           /* if you needed to do something special */
  147.         case updateEvt:
  148.             BeginUpdate((WindowPtr)event->message);
  149.             EndUpdate((WindowPtr)event->message);
  150.         case diskEvt:
  151.             break;
  152.     }
  153. }
  154.  
  155. void DoMenuCommand(long menuResult)
  156. {
  157.     short        menuID;                /* the resource ID of the selected menu */
  158.     short        menuItem;            /* the item number of the selected menu */
  159.     
  160.     menuID = HiWord(menuResult);    /* use macros to get item & menu number */
  161.     menuItem = LoWord(menuResult);
  162.     
  163.     switch (menuID) 
  164.     {
  165.         case mApple:
  166.             switch (menuItem) 
  167.             {
  168.                 case iAbout:
  169.                     DoAboutBox();
  170.                     break;
  171.                 default:
  172.                     break;
  173.             }
  174.             break;
  175.         case mFile:
  176.             switch (menuItem) 
  177.             {
  178.                 case iQuit:
  179.                     ExitToShell();
  180.                     break;
  181.             }
  182.             break;
  183.         case mEdit:
  184.             break;
  185.         case mHello:
  186.             switch (menuItem) 
  187.             {
  188.                 case iThis:
  189.                         SysBeep(2);
  190.                     break;
  191.                 case iThat:
  192.                     SysBeep(2);
  193.                     break;
  194.             }
  195.             break;
  196.     }
  197.     HiliteMenu(0);            /* unhighlight what MenuSelect (or MenuKey) hilited */
  198. }
  199.  
  200. void DoAboutBox(void)
  201. {
  202.     //Carbon currently has an event problem with modal dialogs
  203.     //will put this back soon...
  204.    
  205.    //(void) Alert(kAboutBox, nil);  // simple alert dialog box
  206. }